﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

namespace 自定义TaskSchedule
{
    class Program
    {
        static void Main(string[] args)
        {
            var task = new Task(() =>
            {
                Console.WriteLine("Hello world!");

            });
            task.Start(new MyTaskSchedul());
            Console.Read();

        }
    }


    public class MyTaskSchedul : TaskScheduler
    {
        /// <summary>
        /// 给debug用
        /// </summary>
        /// <returns></returns>
        protected override IEnumerable<Task> GetScheduledTasks()
        {
           return Enumerable.Empty<Task>();
        }
        /// <summary>
        /// 执行task
        /// </summary>
        /// <param name="task"></param>
        protected override void QueueTask(Task task)
        {
            //在thread里面执行 task
            var t = new Thread(() =>
            {
                TryExecuteTask(task);
            });
            t.Start();

        }
        /// <summary>
        /// 同步执行
        /// </summary>
        /// <param name="task"></param>
        /// <param name="taskWasPreviouslyQueued"></param>
        /// <returns></returns>
        protected override bool TryExecuteTaskInline(Task task, bool taskWasPreviouslyQueued)
        {
            return true;
        }
    }

}
